/**
 * 整体架构页面
 * @author wenchuan.li
 * @date 2018/11/30 15:37
 */
define(function () {

    var self,
        menus = [],
        count = 0, list = [],
        app = win.app,vue,
        vueData = {
            menus: [],
            count: 0,
            list: []
        }
    ;

    function initLayUi() {

        layui.extend({
            setter: "index/config",
            admin: "index/admin",
            view: "index/view"
        }).define(["setter", "admin"], function (exports) {

            var setter = layui.setter, //e
                element = layui.element, //i
                admin = layui.admin, //n
                tabsPage = admin.tabsPage, //t
                view = layui.view, //d

                openTabsPage = function (_url, _title) {
                    var l,
                        b = $("#LAY_app_tabsheader>li"),
                        y = _url.replace(/(^http(s*):)|(\?[\s\S]*$)/g, "");

                    b.each(function (e) {
                        var li = $(this),
                            layid = li.attr("lay-id");
                        if (layid === _url) {
                            l = true;
                            tabsPage.index = e;
                        }
                    });

                    _title = _title || "新标签页";

                    if (setter.pageTabs) {
                        l || ($(appBody).append(['<div class="layadmin-tabsbody-item layui-show">',
                            '<iframe src="' + _url + '" frameborder="0" class="layadmin-iframe"></iframe>', "</div>"
                        ].join("")),
                            tabsPage.index = b.length,
                            element.tabAdd(o, {
                                title: "<span>" + _title + "</span>",
                                id: _url,
                                attr: y
                            }));
                    } else {
                        var u = n.tabsBody(n.tabsPage.index).find(".layadmin-iframe");
                        u[0].contentWindow.location.href = _url
                    }
                    element.tabChange(o, _url);
                    admin.tabsBodyChange(tabsPage.index, {
                        url: _url,
                        text: _title
                    });
                },
                appBody = "#E4j_app_body",//s
                o = "layadmin-layout-tabs";

            admin.screen() < 2 && admin.sideFlexible();

            view().autoRender();
            //我的资料
            admin.events.info = function () {
                Index.info();
            };
            //修改密码
            admin.events.password = function () {
                Index.password();
            };
            //退出
            admin.events.logout = function () {
                Index.logout();
            };

            admin.events.detail = function (data) {
                Index.detail(data);
            };

            exports("index", {
                openTabsPage: openTabsPage
            });

            layui.use(['table', 'laydate'], $.noop);

            layui.use(['form'],function () {
                var form = layui.form;
                form.on('select(menusList)', function (data) {
                    openTabsPage(data.value, data.elem[data.elem.selectedIndex].text);
                });
            });
        });
    }

    var Index = app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'index',
                _loadTplsFull: [{
                    dom: 'password', tpl: 'system/password', time: 3000
                }, {
                    dom: 'info', tpl: 'system/info', time: 2000
                }, {
                    dom: 'message_detail', tpl: 'system/message_detail', time: 2000
                }]
            }, win);
        },
        getMenus: function () {
            app.ajax({
                xurl: 'index/getMenus',
                xTime: '2m',
                ocache: true,
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        var item, _menus = [];
                        var string1 = "";
                        var string2 = "";
                        string1 = string1+ "<option  value ='home/console.html'>搜索菜单</option>";
                        string2 = string2 + '<dd lay-value="home/console.html">搜索菜单</dd>';
                        datas.forEach(function (v) {
                            if (v.IS_LEAF == '0') {
                                if (item) {
                                    _menus.push(item);
                                }
                                item = {p: v, c: []};
                            } else {
                                var str = v.CASCADE_ID.substring(0, v.CASCADE_ID.lastIndexOf('.'));
                                var flag = false;
                                datas.forEach(function (t) {
                                    if(str === t.CASCADE_ID){
                                        flag = true;
                                    }
                                });
                                if(flag) {
                                    item.c.push(v);
                                    string1 = string1+ "<option  value =" + v.URL +  " >" + v.NAME + "</option>";
                                    string2 = string2 + '<dd lay-value="' + v.URL + '">' + v.NAME + '</dd>';
                                }
                            }
                        });
                        $("#menus_select").next().children().eq(1).html(string2);
                        $("#menus_select").html(string1);
                        _menus.push(item);
                        _menus.sort(function (a, b) {
                            if (a.p.SORT_NO < b.p.SORT_NO) {
                                return -1;
                            }
                            if (a.p.SORT_NO > b.p.SORT_NO) {
                                return 1;
                            }
                            return 0;
                        });
                        _menus.forEach(function (v) {
                            v.c.sort(function (a, b) {
                                if (a.SORT_NO < b.SORT_NO) {
                                    return -1;
                                }
                                if (a.SORT_NO > b.SORT_NO) {
                                    return 1;
                                }
                                return 0;
                            });
                        });
                        vueData.menus.pushArray(_menus);
                    }
                    initLayUi();
                }
            });
        },

        password: function () {
            self._openTplFull({
                tag: 'password',
                index: 0,
                openOpts: {
                    title: '修改密码',
                    area: ['600px', '300px']
                }
            });
        },

        info: function () {
            self._openTplFull({
                tag: 'info',
                index: 1,
                openOpts: {
                    title: '我的资料',
                    area: ['600px', '500px']
                }
            });
        },

        logout: function () {
            app.ajax({
                xurl: 'logout',
                loading: true,
                cb: function (result) {
                    if (result.ok) {
                        location.href = g_ctxPath + 'login';
                    }
                }
            });

            app.lStorage.remove('index2FgetMenus');
        },
        detail: function (data) {
            self._openTplFull({
                tag: 'message_detail',
                tagData: {
                    GWDI001: data.attr('lay-data')
                },
                index: 2,
                openOpts: {
                    title: '我的消息',
                    area: ['600px', '500px']
                }
            });

        },
        getGwdi: function () {
            app.ajax({
                xurl: "admin/getGwdiList",
                async: false,
                cb: function (result) {
                    if (result.ok) {
                        vueData.count = 0;
                        vueData.list = [];
                        vueData.count = result.data.data.length;
                        if (result.data.data.length <= 5) {
                            vueData.list = result.data.data;
                        } else {
                            for (var i = 0; i < 5; i++) {
                                vueData.list.push(result.data.data[i]);
                            }
                        }
                    }
                }
            });
        },

        initPlay: function () {
            var text = '';
            app.ajax({
                xurl: "admin/getGwdiList",
                xdata: {GWDI007: 'N'},
                async: false,
                cb: function (result) {
                    if (result.ok) {
                        var count = 0;
                        var list = [];
                        count = result.data.data.length;
                        if (result.data.data.length <= 5) {
                            list = result.data.data;
                        } else {
                            for (var i = 0; i < 5; i++) {
                                list.push(result.data.data[i]);
                            }
                        }
                        for (var i = 0; i < list.length; i++) {
                            text = text + '作业名称:' + list[i].GWDI002 + ',单号:' + list[i].GWDI003;
                        }
                        if (count > list.length) {
                            text = text + '<p lay-href="crm/gwai880">查看更多</p>'
                        }
                        app.ajax({
                            xurl: 'crm/gwai880/update',
                            cb: function (result) {
                                if (result.ok) {

                                }
                            }
                        });
                        layui.use(['layer'], function () {
                            if(list.length > 0) {
                                layui.layer.open({
                                    type: 1
                                    , time: 600000
                                    , offset: 'lb'
                                    , id: 'layerDemo'
                                    , content: '<div style="padding: 20px 20px;">' + text + '</div>'
                                    , btnAlign: 'c' //按钮居中
                                    , shade: 0 //不显示遮罩
                                });
                            }
                        });
                    }
                }
            });
        },

        play: function () {
            setInterval(function () {
                var text = '';
                app.ajax({
                    xurl: "admin/getGwdiList",
                    xdata: {GWDI007: 'N'},
                    async: false,
                    cb: function (result) {
                        if (result.ok) {
                            var count = 0;
                            var list = [];
                            count = result.data.data.length;
                            if (result.data.data.length <= 5) {
                                list = result.data.data;
                            } else {
                                for (var i = 0; i < 5; i++) {
                                    list.push(result.data.data[i]);
                                }
                            }
                            for (var i = 0; i < list.length; i++) {
                                text = text + '作业名称:' + list[i].GWDI002 + ',单号:' + list[i].GWDI003;
                            }
                            if (count > list.length) {
                                text = text + '<p lay-href="crm/gwai880">查看更多</p>'
                            }
                            app.ajax({
                                xurl: 'crm/gwai880/update',
                                cb: function (result) {
                                    if (result.ok) {

                                    }
                                }
                            });
                            if(list.length > 0) {
                                layui.layer.open({
                                    type: 1
                                    , time: 600000
                                    , offset: 'lb'
                                    , id: 'layerDemo'
                                    , content: '<div style="padding: 20px 20px;">' + text + '</div>'
                                    , btnAlign: 'c' //按钮居中
                                    , shade: 0 //不显示遮罩
                                });
                            }
                            self.getGwdi();
                        }
                    }
                });
            }, 1800000)
        },

        _readyBefore: function () {

            self.getMenus();

            // self.play();
            // self.getGwdi();
            app.ajax({
                xdata: {
                    GWDH002: '3'
                },
                xurl: "admin/getNotices",
                cb: function (result) {
                    if (result.ok) {
                        console.log(result)
                    }
                }
            });
        },
        _ready: function () { // dom载入完成后执行

            vue = new Vue({
                el: '#E4j_app',
                data: vueData
            });
            // self.initPlay();
        },
        _lazy: function () { // 延迟500ms加载
            this._super();
            require(['pc/public/others'], $.noop);
            $('#page_admin').attr('src', g_ctxPath + 'admin');
        }
    });
});